package cn.iocoder.yudao.module.biz.controller.admin.risk.riskWarningSign;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.biz.controller.admin.risk.riskInfo.vo.BizRiskInfoRespVO;
import cn.iocoder.yudao.module.biz.controller.admin.risk.riskWarningSign.vo.*;
import cn.iocoder.yudao.module.biz.convert.risk.riskWarningSign.BizRiskWarningSignConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.risk.riskWarningSign.BizRiskWarningSignDO;
import cn.iocoder.yudao.module.biz.service.risk.riskWarningSign.BizRiskWarningSignService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

@Api(tags = "管理后台 - 警示标志")
@RestController
@RequestMapping("/biz/risk-warning-sign")
@Validated
public class BizRiskWarningSignController {

    @Resource
    private BizRiskWarningSignService riskWarningSignService;

    @PostMapping("/create")
    @ApiOperation("创建警示标志")
    @PreAuthorize("@ss.hasPermission('biz:risk-warning-sign:create')")
    public CommonResult<String> createRiskWarningSign(@Valid @RequestBody BizRiskWarningSignCreateReqVO createReqVO) {
        return success(riskWarningSignService.createRiskWarningSign(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新警示标志")
    @PreAuthorize("@ss.hasPermission('biz:risk-warning-sign:update')")
    public CommonResult<Boolean> updateRiskWarningSign(@Valid @RequestBody BizRiskWarningSignUpdateReqVO updateReqVO) {
        riskWarningSignService.updateRiskWarningSign(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除警示标志")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:risk-warning-sign:delete')")
    public CommonResult<Boolean> deleteRiskWarningSign(@RequestParam("id") String id) {
        riskWarningSignService.deleteRiskWarningSign(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得警示标志")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:risk-warning-sign:query')")
    public CommonResult<BizRiskWarningSignRespVO> getRiskWarningSign(@RequestParam("id") String id) {
        BizRiskWarningSignDO riskWarningSign = riskWarningSignService.getRiskWarningSign(id);
        return success(BizRiskWarningSignConvert.INSTANCE.convert(riskWarningSign));
    }

    @GetMapping("/list")
    @ApiOperation("获得警示标志列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:risk-warning-sign:query')")
    public CommonResult<List<BizRiskWarningSignRespVO>> getRiskWarningSignList(@RequestParam("ids") Collection<String> ids) {
        List<BizRiskWarningSignDO> list = riskWarningSignService.getRiskWarningSignList(ids);
        return success(BizRiskWarningSignConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得警示标志分页")
    @PreAuthorize("@ss.hasPermission('biz:risk-warning-sign:query')")
    public CommonResult<PageResult<BizRiskWarningSignRespVO>> getRiskWarningSignPage(@Valid BizRiskWarningSignPageReqVO pageVO) {
        PageResult<BizRiskWarningSignDO> pageResult = riskWarningSignService.getRiskWarningSignPage(pageVO);
        return success(BizRiskWarningSignConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/getWarningSignPage")
    @ApiOperation("获得警示标志分页")
    @PreAuthorize("@ss.hasPermission('biz:risk-warning-sign:query')")
    public CommonResult<PageResult<BizRiskWarningSignRespVO>> getWarningSignPage(@Valid BizRiskWarningSignPageReqVO pageVO) {
        PageResult<BizRiskWarningSignRespVO> result=new PageResult<>();
        Integer pageNo=pageVO.getPageNo();
        Integer pageSize=  pageVO.getPageSize();
        //设置为limit开始的条数
        pageVO.setPageNo((pageNo-1)*pageSize);
        pageVO.setDeptId(SecurityFrameworkUtils.getLoginUser().getDeptId()+"");
        List<BizRiskWarningSignRespVO> list = riskWarningSignService.getWarningSignPage(pageVO);
        Integer count= riskWarningSignService.getWarningSignCount(pageVO);
        result.setList(list);
        result.setTotal((long) count);
        return success(result);
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出警示标志 Excel")
    @PreAuthorize("@ss.hasPermission('biz:risk-warning-sign:export')")
    @OperateLog(type = EXPORT)
    public void exportRiskWarningSignExcel(@Valid BizRiskWarningSignExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BizRiskWarningSignDO> list = riskWarningSignService.getRiskWarningSignList(exportReqVO);
        // 导出 Excel
        List<BizRiskWarningSignExcelVO> datas = BizRiskWarningSignConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "警示标志.xls", "数据", BizRiskWarningSignExcelVO.class, datas);
    }

}
